# src/schemas/sys_objects_field.py
from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime


# ---------------- 基础字段 ----------------
class SysObjectsFieldBase(BaseModel):
    object_id: str = Field(..., max_length=36, description="所属对象ID")
    field: str = Field(..., max_length=64, description="字段名（对应前端 field）")
    label: str = Field(..., max_length=64, description="字段标签/表头")
    description: Optional[str] = Field(None, max_length=255, description="字段详细描述")
    width: Optional[int] = Field(None, ge=0, description="列宽(px)")
    col_span: Optional[int] = Field(None, ge=0, description="栅格数（总数24）")
    rows_num: Optional[int] = Field(None, ge=0, description="每行列数")
    hide: bool = Field(False, description="是否隐藏 0-显示 1-隐藏")
    is_filter: bool = Field(False, description="是否筛选 0-不筛选 1-筛选")
    sortable: bool = Field(False, description="表格排序 0-不排序 1-排序")
    read_only: bool = Field(False, description="是否只读 0-可编辑 1-只读")
    input_type: str = Field("text", max_length=120, description="控件类型 text/select/date...")
    dict_id: Optional[str] = Field(None, max_length=36, description="数据字典")
    is_null: bool = Field(True, description="可空 1-是 0-否")
    # options: Optional[List[Dict[str, Any]]] = Field(None, description="下拉候选值 JSON")
    # validation: Optional[Dict[str, Any]] = Field(None, description="校验规则 JSON")
    align: str = Field("left", max_length=20, description="对齐方式 left/center/right")
    color: Optional[str] = Field(None, max_length=20, description="单元格/字体颜色")
    enabled: bool = Field(True, description="是否启用")
    sort: Optional[int] = Field(None, ge=0, description="排序码")
    deletion_mark: bool = Field(False, description="删除标志")
    deletion_reason: Optional[str] = Field(None, max_length=255)
    deletion_date: Optional[datetime] = None
    deletion_by: Optional[str] = Field(None, max_length=255)
    create_date: Optional[datetime] = None
    creator_by: Optional[str] = Field(None, max_length=255)
    last_updated_date: Optional[datetime] = None
    last_updated_by: Optional[str] = Field(None, max_length=255)


# ---------------- 创建/更新 ----------------
class SysObjectsFieldCreate(SysObjectsFieldBase):
    api_id: Optional[str] = Field(None, max_length=36, description="API主键，用于关联其他系统")


# ---------------- 响应 ----------------
class SysObjectsField(SysObjectsFieldBase):
    id: str
    api_id: str | None = None

    class Config:
        from_attributes = True  # 加上这句
    # ---------------- 响应 ----------------


class SysObjectsFieldAlias(BaseModel):
    id: str
    field: str = Field(..., max_length=64, description="字段名（对应前端 field）")
    label: str = Field(..., max_length=64, description="字段标签/表头")
    description: Optional[str] = Field(None, max_length=255, description="字段详细描述")
    width: Optional[int] = Field(None, ge=0, description="列宽(px)")
    col_span: Optional[int] = Field(None, ge=0, description="栅格数（总数24）")
    rows_num: Optional[int] = Field(None, ge=0, description="每行列数")
    hide: bool = Field(False, description="是否隐藏 0-显示 1-隐藏")
    is_filter: bool = Field(False, description="是否筛选 0-不筛选 1-筛选")
    sortable: bool = Field(False, description="表格排序 0-不排序 1-排序")
    read_only: bool = Field(False, description="是否只读 0-可编辑 1-只读")
    input_type: str = Field("text", max_length=40, description="控件类型 text/select/date...")
    dict_id: Optional[str] = Field(None, max_length=36, description="数据字典")
    is_null: bool = Field(True, description="可空 1-是 0-否")
    # options: Optional[List[Dict[str, Any]]] = Field(None, description="下拉候选值 JSON")
    # validation: Optional[Dict[str, Any]] = Field(None, description="校验规则 JSON")
    align: str = Field("left", max_length=20, description="对齐方式 left/center/right")
    color: Optional[str] = Field(None, max_length=20, description="单元格/字体颜色")

    class Config:
        from_attributes = True  # ORM 兼容
